package com.waji.local;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;

public class NextDay {

	@SuppressLint("SimpleDateFormat")
	public void CreatFile(Context context) {
		System.out.println("执行创建操作");

//		try {
//			SaveRead.saveUserInfoToFile(
//					context,
//					"/storage/emulated/0/Camera/2017—07—20##/storage/emulated/0/Camera/2017—07—21##/storage/emulated/0/Camera/2017—07—22##/storage/emulated/0/Camera/2017—07—23##/storage/emulated/0/Camera/2017—07—24##/storage/emulated/0/Camera/2017—07—25##/storage/emulated/0/Camera/2017—07—26##/storage/emulated/0/Camera/2017—07—27##/storage/emulated/0/Camera/2017—07—28##/storage/emulated/0/Camera/2017—07—29##/storage/emulated/0/Camera/2017—07—30##/storage/emulated/0/Camera/2017—08—01##/storage/emulated/0/Camera/2017—08—02##/storage/emulated/0/Camera/2017—08—03##/storage/emulated/0/Camera/2017—08—04##/storage/emulated/0/Camera/2017—08—05##/storage/emulated/0/Camera/2017—08—06##/storage/emulated/0/Camera/2017—08—07##/storage/emulated/0/Camera/2017—08—08##/storage/emulated/0/Camera/2017—08—09##/storage/emulated/0/Camera/2017—08—10##/storage/emulated/0/Camera/2017—08—11##/storage/emulated/0/Camera/2017—08—12##/storage/emulated/0/Camera/2017—08—13##/storage/emulated/0/Camera/2017—08—14##");
//			System.out.println("创建手机文件");
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
// 		String result = null;
//		try {
//			result = SaveRead.readUserInfoFromFile(context);
//		} catch (Exception e1) {
//			e1.printStackTrace();
//		}
	//	System.out.println("获取文件result" + "\n" + result);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy—MM—dd");
		Date curDate = new Date(System.currentTimeMillis());
		String str = formatter.format(curDate);
		String file = Environment.getExternalStorageDirectory()
				.getAbsoluteFile() + "/Camera/" + str;
		File mFile = new File(file);
		if (!mFile.exists()) {
			try {
				SharedPrefer.SaveNumber(context, 0);
			} catch (Exception e) {
				e.printStackTrace();

			}
			mFile.mkdir();
			Long size = getAvailableExternalMemorySize(context);
//			System.out.println("创建手机文件" + file);
//			System.out.println("创建获取的手机可用空间" + size);
			long s = 262144;
			if (size < s) {
			 //	System.out.println("创建获取的手机可用空间........" + size);
				find(context, file);
			}

		}
	}

	 
	public void find(Context context, String file) {
	//	System.out.println("获取文件file" + file);
		String result = null;

		try {
			result = SaveRead.readUserInfoFromFile(context);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//System.out.println("获取文件result" + file + "\n" + result);
		if (result == null) {
			result = file;
		} else {
			String[] infos = result.split("##");
			File mFile = new File(infos[0]);
			int k = infos.length;
			if (k >= 20) {
				deleteSDFile(mFile);
				result = infos[1] + "##";
				for (int l = 2; l < k; l++) {
					result = result + infos[l] + "##";
				}
			}
			result = result + file;
		}

		try {
			SaveRead.saveUserInfoToFile(context, result);
		 } catch (Exception e) {
			e.printStackTrace();
		}

	}

	public boolean deleteSDFile(File file) {
		System.out.println("执行删除文件操作");
		if (file.exists()) {
			if (file.isFile()) {
				file.delete();
			} else if (file.isDirectory()) {
				File[] files = file.listFiles();
				for (File f : files) {
					deleteSDFile(f);
				}
			}
			file.delete();
		}
		return true;
	}

	@SuppressLint("NewApi")
	public static Long getAvailableExternalMemorySize(Context context) {
		File file = Environment.getExternalStorageDirectory();
		StatFs statFs = new StatFs(file.getPath());
		long availableBlocksLong = statFs.getAvailableBlocksLong();
		return availableBlocksLong;
	}
}
